package zdream.control.foe.mm2pipi;

import zdream.control.world.buff.AttackModule;
import zdream.control.world.buff.HealthModule;
import zdream.control.world.buff.JumpModule;
import zdream.control.world.MFoe;
import zdream.control.world.MHookType;
import zdream.control.world.MHooks;
import zdream.control.world.buff.StageModule;
import zdream.control.world.TerrainHandler;

import static zdream.control.world.MFoeConstants.*;

/**
 * <p>2 代飞鸟带着的蛋.
 * <p>扔下来前:
 * <li>位置跟随飞鸟. 当鸟死了之后蛋也会没.
 * <li>生命值: 1, 碰撞伤害: 2(仅下落阶段), 碰撞 camp=3
 * </li>
 * <p>扔下来后:
 * <li>开始受地形影响, 做自由落体运动. 水平方向无速度.
 * <li>不会跟着鸟死后自毁
 * <li>每步判定是否砸到东西 (包括地形和别的怪, 触发的第一步时间不判断飞鸟)
 * 一旦砸到东西, 给予 camp=3 数值 2 的无差别伤害. 如果砸到地形则不释放伤害.
 * 砸到后自己碎裂, 产生 8 只 MM2PipiChick 和 2 个贴图 foe.
 * </li></p>
 *
 * @author Zdream
 * @since 0.0.1
 * @date 2020-06-11 (created)
 * @date 2022-06-11 (last modified)
 */
public class MM2PipiEgg extends MFoe {
	public static final String NAME = "MM2PipiEgg";
	public final MM2Pipi parent;

	public MM2PipiEgg(MM2Pipi parent) {
		super(parent.world);
		this.parent = parent;
		this.type = TYPE_FOE;
		this.side = parent.side;
	}

	@Override
	public String foeName() {
		return NAME;
	}

	@Override
	public void init() {
		terrainHandler = new TerrainHandler(world);
		createNormalPattern()
				.withRectangle(-0.375f, -0.25f, 0.75f, 0.5f)
				.apply();

		attackModule = buildAttackModule()
				.damage(AttackModule.DAMAGE_DISABLE)
				.damageLevel(DAMAGE_LEVEL_BODY)
				.targetTypesIsEntity()
				.create();
		jumpModule = buildJumpModule()
				.defineAsPassiveMode()
				.setTerrainHandler(terrainHandler)
				.setJumpMaxDy(0)
				.create();
		healthModule = buildHealthModule()
				.outsideDestroyThreshold(1000)
				.hpMax(1)
				.create();

		onHook(MHookType.TICK)
				.handle(this::checkBroken)
				.buildAndSave();
		onHook(MHookType.APPLY_DAMAGE)
				.withPriority(100)
				.handle(this::onAttackFinished)
				.buildAndSave();
	}

	public AttackModule attackModule;
	public HealthModule healthModule;
	public JumpModule jumpModule;
	public StageModule stageModule;

	public void checkBroken() {
		if (!abandoned) {
			return;
		}

		flush();
		if (leftTouched || rightTouched || topTouched || bottomTouched) {
			broken();
			return;
		}

		if (terrainHandler.isBoxOverlap(this)) {
			broken();
		}
	}

	/* **********
	 *   行为   *
	 ********** */
	/**
	 * false: 跟随阶段
	 *  true: 自由落体阶段
	 */
	public boolean abandoned = false;

	/**
	 * 扔蛋之后, 鸟会调用它
	 */
	void thrown() {
		abandoned = true;
		attackModule.setDamage(2, DAMAGE_LEVEL_BODY);
		jumpModule.passive = false;

		terrainHandler.freshBox(this);
		onHook(MHookType.APPLY_DAMAGE)
				.withPriority(-100)
				.handle(this::needAttack)
				.buildAndSave();
	}

	protected void needAttack(MHooks.MHookContext rawContext) {
		final MHooks.DamageContext context = rawContext.as();

		if (!abandoned) {
			context.abort();
			return;
		}

		MFoe target = context.defender;
		if (target.foeName().equals(MM2Pipi.NAME)) {
			context.abort();
		}
	}

	protected void onAttackFinished() {
		broken();
	}

	/**
	 * 碎了, 要出鸟了
	 */
	private void broken() {
		// 产生 8 只鸟
		float[][] is = new float[][] {
				{-4, 0},
				{-3.6f, 1.744f},
				{-2.4f, 3.2f},
				{-0.84f, 3.911f},
				{0.84f, 3.911f},
				{2.4f, 3.2f},
				{3.6f, 1.744f},
				{4, 0},
		};
		for (int i = 0; i < is.length; i++) {
			float[] xy = is[i];

			MM2PipiChick chick = new MM2PipiChick(this);
			chick.setInitVelocity(xy[0], xy[1]);
			world.createFoe(chick, anchorX, anchorY, orientation);
		}

		// TODO 产生 2 个碎片 Foe
		destroy();
	}

	/**
	 * 由鸟调用, 设置位置.
	 */
	void setPosition(float pipiX, float pipiY) {
		setAnchor(pipiX, pipiY - 0.25f);
	}
}
